<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-07-31 17:46:49
 * @LastEditTime: 2020-03-25 11:29:30
 * @LastEditors: Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../css/base.css" />
    <script type="text/javascript" src="../script/jquery.js"></script>
    <script type="text/javascript" src="../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../script/vue.min.js"></script>
    <script type="text/javascript" src="../script/vant.min.js"></script>
    <script type="text/javascript" src="../script/fastclick.js"></script>
    <script type="text/javascript" src="../script/base.js"></script>
    <style type="text/css">

    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="init-full">

        </div>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        //$('.init-full').show();
        var w = 0;

        api.addEventListener({
            name: 'searchAddress'
        }, function (ret, err) {
            var addressKeyWord = ret.value.key;
            map.getCoordsFromName({
                address: addressKeyWord
            }, function (ret, err) {
                if (ret.status) {
                    map.setCenter({
                        coords: {
                            lon: ret.lon,
                            lat: ret.lat
                        }
                    });
                }
            });

        })



        api.addEventListener({
            name: 'saveLocation'
        }, function (ret, err) {
            vm.saveLocation();
        })

        api.addEventListener({
            name: 'setUserPopup'
        }, function (ret, err) {
            if (window.localStorage.getItem('visitData')) {
                var visitData = JSON.parse(window.localStorage.getItem('visitData'));
                map.setBubble({
                    id: 9999,
                    content: {
                        title: '当前拜访：' + visitData.S_ClientName,
                        subTitle: '签到：' + visitData.DateTime.split(' ')[0] + ' - '
                    },
                    styles: {
                        titleColor: '#000',
                        titleSize: 14,
                        subTitleColor: '#999',
                        subTitleSize: 12,
                        illusAlign: 'left',
                        w: vm.getStringLength('当前拜访：' + visitData.S_ClientName) * 17,
                        h: 100
                    }
                }, function (ret) {
                    checkCustomerData();

                });
                map.popupBubble({
                    id: 9999
                });
            } else {
                map.closeBubble({
                    id: 9999
                });
            }
        })

        api.addEventListener({
            name: 'searchVisitList'
        }, function (ret, err) {
            vm.searchVisitList(ret.value.date);
        })

        api.addEventListener({
            name: 'setCenter'
        }, function (ret, err) {

            map.getLocation({
                accuracy: '10m',
                autoStop: true
            }, function (ret, err) {
                map.setCenter({
                    coords: {
                        lon: ret.lon,
                        lat: ret.lat
                    },
                    animation: false
                });
            })

        })

        api.addEventListener({
            name: 'selectVisitRow'
        }, function (ret, err) {
            var row = ret.value.selectRow;
            map.setBubble({
                id: row.id,
                content: {
                    title: row.ClientName,
                    subTitle: '签到：' + row.StartTime.split(' ')[1] + ' - ' + (row.EndTime.split(' ')[1] || '')
                },
                styles: {
                    titleColor: '#000',
                    titleSize: 14,
                    subTitleColor: '#999',
                    subTitleSize: 12,
                    illusAlign: 'left',
                    w: vm.getStringLength(row.ClientName) * 17,
                    h: 100
                }
            }, function (ret) {

            });
            map.popupBubble({
                id: row.id
            });
        })

        function checkCustomerData() {
            if (window.localStorage.getItem('customerRow')) {
                var customerRow = JSON.parse(window.localStorage.getItem('customerRow'));
                var frames = api.frames();
                var new_frame = frames.filter(function (row) {
                    return row.name == 'customerData';
                })
                if (new_frame.length == 0) {
                    var appTop_ = parseInt(window.localStorage.getItem('safeAreaTop'));
                    var y = appTop_ ? (appTop_ + 40) : (appTop_ + 25);
                    api.openFrame({
                        name: 'customerData',
                        url: 'common/customerData.html',
                        rect: {
                            x: 5,
                            y: y + 5,
                            w: api.winWidth - 10,
                            h: 96
                        },
                        pageParam: {
                            customerData: customerRow
                        }
                    });
                } else {
                    api.sendEvent({
                        name: 'setcustomerData',
                        extra: {
                            customerData: customerRow
                        }
                    });
                }
            }
        }

        var userInfo = FLD.getUserInfo();
        var map = api.require('bMap');
        if (api.systemType == 'android') {
			map.setAgreePrivacy({
			agree:true
		});	
		}	
        map.initMapSDK(function (ret) { });
        
        vm = new Vue({
            el: '#app',
            data: {
                showLoading: false,
                ifConfirm: false,
                customerData: api.pageParam.customerData
            },
            created: function () {
                this.openMap();
            },
            methods: {
                getStringLength(str) {
                    if (api.systemType == 'ios') {
                        return 16;
                    } else {
                        var slength = 0;
                        for (i = 0; i < str.length; i++) {
                            if ((str.charCodeAt(i) >= 0) && (str.charCodeAt(i) <= 255))
                                slength = slength + 1;
                            else
                                slength = slength + 2;
                        }
                        if (slength < 28) {
                            slength = 28;
                        }
                        return slength;
                    }

                },
                openMap() {
                    var that = this;
                    var y = api.pageParam.y;
                    var b = (api.pageParam.type == '定位' || api.pageParam.type == '分布' || api.pageParam.type == '商城分布') ? 0 : 190;
                    map.open({
                        rect: {
                            x: 0,
                            y: y,
                            w: api.winWidth,
                            h: api.winHeight - y - b
                        },
                        center: {
                            lon: 116.39432327,
                            lat: 39.88933191
                        },
                        zoomLevel: 15,
                        showUserLocation: (api.pageParam.type == '定位' || api.pageParam.type == '分布' || api.pageParam.type == '商城分布') ? true : false,
                        fixed: true
                    }, function (ret) {
                        if (api.pageParam.type == '拜访') {
                            var ifFirst = true;
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: false
                            }, function (ret, err) {
                                if (ret.status) {

                                    if (!ifFirst) {
                                        map.setAnnotationCoords({
                                            id: 9999,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        });
                                        return false;
                                    }
                                    ifFirst = false;

                                    var setPopup = function () {
                                        if (window.localStorage.getItem('visitData')) {
                                            var visitData = JSON.parse(window.localStorage.getItem('visitData'));
                                            map.setBubble({
                                                id: 9999,
                                                content: {
                                                    title: '当前拜访：' + visitData.S_ClientName,
                                                    subTitle: '签到：' + visitData.DateTime.split(' ')[0] + ' - '
                                                },
                                                styles: {
                                                    titleColor: '#000',
                                                    titleSize: 14,
                                                    subTitleColor: '#999',
                                                    subTitleSize: 12,
                                                    illusAlign: 'left',
                                                    w: that.getStringLength('当前拜访：' + visitData.S_ClientName) * 17
                                                }
                                            }, function (ret) {
                                                checkCustomerData();
                                            });
                                            map.popupBubble({
                                                id: 9999
                                            });
                                        } else {
                                            map.closeBubble({
                                                id: 9999
                                            });
                                        }
                                    }



                                    map.addAnnotations({
                                        annotations: [{
                                            id: 9999,
                                            size: 40,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }],
                                        icon: 'widget://image/u.png',
                                        draggable: true
                                    }, function (ret) {
                                        if (ret) {
                                            setPopup();
                                        }
                                    });

                                    setTimeout(function () {
                                        setPopup();
                                        if (window.localStorage.getItem('visitData')) {
                                            checkCustomerData();
                                        }

                                    }, 1000)

                                    map.setCenter({
                                        coords: {
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }
                                    });

                                    var rangeJSON = that.calLatLngByDistance(ret.lat, ret.lon, 500);

                                    var obj = {
                                        S_Type: 17,
                                        S_Cont: '',
                                        S_BranchID: userInfo.S_BranchID,
                                        S_SXZ: '',
                                        S_StockID: userInfo.S_StockID,
                                        S_CargoID: userInfo.S_CargoID,
                                        S_BarCode: '',
                                        S_PriceN: userInfo.S_PriceN,
                                        S_ProductID: 0
                                    }
                                    FLD.ajax('AP_BasList', obj, function (res) {
                                        var annotations = [];
                                        var z = -1;
                                        for (var i in res.ds) {
                                            if (res.ds[i].Longitude && res.ds[i].Latitude) {
                                                if (res.ds[i].Longitude > rangeJSON.newLng1 && res.ds[i].Longitude < rangeJSON.newLng2 && res.ds[i].Latitude > rangeJSON.newLat1 && res.ds[i].Latitude < rangeJSON.newLat2) {
                                                    z = i;
                                                }
                                                annotations.push({
                                                    id: i,
                                                    size: 40,
                                                    lon: res.ds[i].Longitude,
                                                    lat: res.ds[i].Latitude
                                                })
                                            }

                                        }
                                        if (z > -1 && !window.localStorage.getItem('visitData')) {
                                            window.localStorage.setItem('customerRow', JSON.stringify(res.ds[z]));
                                            api.openFrame({
                                                name: 'customerVisitForm',
                                                url: 'customerVisitForm.html',
                                                rect: {
                                                    x: 0,
                                                    y: api.winHeight - 190,
                                                    w: api.winWidth,
                                                    h: 190
                                                },
                                                pageParam: {
                                                    defaultCustomerData: res.ds[z]
                                                }
                                            });
                                            api.openFrame({
                                                name: 'customerData',
                                                url: 'common/customerData.html',
                                                rect: {
                                                    x: 5,
                                                    y: y + 5,
                                                    w: api.winWidth - 10,
                                                    h: 96
                                                },
                                                pageParam: {
                                                    customerData: res.ds[z]
                                                }
                                            });
                                        } else {
                                            api.openFrame({
                                                name: 'customerVisitForm',
                                                url: 'customerVisitForm.html',
                                                rect: {
                                                    x: 0,
                                                    y: api.winHeight - 190,
                                                    w: api.winWidth,
                                                    h: 190
                                                }
                                            });
                                        }


                                        map.addAnnotations({
                                            annotations: annotations,
                                            icon: 'widget://image/customer-sign.png',
                                            draggable: false
                                        }, function (ret) {
                                            if (ret) {

                                                var frames = api.frames();
                                                var new_frame = frames.filter(function (row) {
                                                    return row.name == 'customerData';
                                                })
                                                if (new_frame.length == 0) {
                                                    var appTop_ = parseInt(window.localStorage.getItem('safeAreaTop'));
                                                    var y = appTop_ ? (appTop_ + 40) : (appTop_ + 25);
                                                    api.openFrame({
                                                        name: 'customerData',
                                                        url: 'common/customerData.html',
                                                        rect: {
                                                            x: 5,
                                                            y: y + 5,
                                                            w: api.winWidth - 10,
                                                            h: 96
                                                        },
                                                        pageParam: {
                                                            customerData: res.ds[ret.id]
                                                        }
                                                    });
                                                } else {
                                                    api.sendEvent({
                                                        name: 'setcustomerData',
                                                        extra: {
                                                            customerData: res.ds[ret.id]
                                                        }
                                                    });
                                                }




                                            }
                                        });




                                        // map.addAnnotations({
                                        //     annotations: annotations,
                                        //     icon: 'widget://image/customer-sign.png',
                                        //     draggable: false
                                        // }, function (ret) {
                                        //     if (ret) {

                                        //         var frames = api.frames();
                                        //         var new_frame = frames.filter(function (row) {
                                        //             return row.name == 'customerData';
                                        //         })
                                        //         if (new_frame.length == 0) {
                                        //             var appTop_ = parseInt(window.localStorage.getItem('safeAreaTop'));
                                        //             var y = appTop_ ? (appTop_ + 40) : (appTop_ + 25);
                                        //             api.openFrame({
                                        //                 name: 'customerData',
                                        //                 url: 'common/customerData.html',
                                        //                 rect: {
                                        //                     x: 5,
                                        //                     y: y + 5,
                                        //                     w: api.winWidth - 10,
                                        //                     h: 96
                                        //                 },
                                        //                 pageParam: {
                                        //                     customerData: res.ds[ret.id]
                                        //                 }
                                        //             });
                                        //         } else {
                                        //             api.sendEvent({
                                        //                 name: 'setcustomerData',
                                        //                 extra: {
                                        //                     customerData: res.ds[ret.id]
                                        //                 }
                                        //             });
                                        //         }




                                        //     }
                                        // });
                                    })
                                }
                            });


                            api.openFrame({
                                name: 'userLocation',
                                url: 'component/userLocation.html',
                                rect: {
                                    x: 10,
                                    y: api.winHeight - 60,
                                    w: 40,
                                    h: 40
                                }
                            });
                        } else if (api.pageParam.type == '分布') {
                            var ifFirst = true;
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: false
                            }, function (ret, err) {
                                if (ret.status) {

                                    if (!ifFirst) {
                                        map.setAnnotationCoords({
                                            id: 9999,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        });
                                        return false;
                                    }
                                    ifFirst = false;

                                    var setPopup = function () {
                                        if (window.localStorage.getItem('visitData')) {
                                            var visitData = JSON.parse(window.localStorage.getItem('visitData'));
                                            map.setBubble({
                                                id: 9999,
                                                content: {
                                                    title: '当前拜访：' + visitData.S_ClientName,
                                                    subTitle: '签到：' + visitData.DateTime.split(' ')[0] + ' - '
                                                },
                                                styles: {
                                                    titleColor: '#000',
                                                    titleSize: 14,
                                                    subTitleColor: '#999',
                                                    subTitleSize: 12,
                                                    illusAlign: 'left',
                                                    w: that.getStringLength('当前拜访：' + visitData.S_ClientName) * 17
                                                }
                                            }, function (ret) {
                                                checkCustomerData();
                                            });
                                            map.popupBubble({
                                                id: 9999
                                            });
                                        } else {
                                            map.closeBubble({
                                                id: 9999
                                            });
                                        }
                                    }



                                    map.addAnnotations({
                                        annotations: [{
                                            id: 9999,
                                            size: 40,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }],
                                        icon: 'widget://image/u.png',
                                        draggable: true
                                    }, function (ret) {
                                        if (ret) {
                                            // setPopup();
                                        }
                                    });

                                    setTimeout(function () {
                                        // setPopup();
                                        // if (window.localStorage.getItem('visitData')) {
                                        //     checkCustomerData();
                                        // }

                                    }, 1000)

                                    map.setCenter({
                                        coords: {
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }
                                    });

                                    var rangeJSON = that.calLatLngByDistance(ret.lat, ret.lon, 500);

                                    var obj = {
                                        S_Type: 18,
                                        S_Cont: '',
                                        S_BranchID: userInfo.S_BranchID,
                                        S_SXZ: '',
                                        S_StockID: userInfo.S_StockID,
                                        S_CargoID: userInfo.S_CargoID,
                                        S_BarCode: '',
                                        S_PriceN: userInfo.S_PriceN,
                                        S_ProductID: 0
                                    }
                                    FLD.ajax('AP_BasList', obj, function (res) {
                                        var annotations = [];
                                        var z = -1;
                                        for (var i in res.ds) {
                                            if (res.ds[i].BNType == 1) {
                                                annotations.push({
                                                    id: i,
                                                    size: 30,
                                                    lon: res.ds[i].Longitude,
                                                    lat: res.ds[i].Latitude,
                                                    icon: 'widget://image/bf-sign.png'
                                                })
                                            }
                                            if (res.ds[i].BNType == 2) {
                                                annotations.push({
                                                    id: i,
                                                    size: 40,
                                                    lon: res.ds[i].Longitude,
                                                    lat: res.ds[i].Latitude,
                                                    icon: 'widget://image/fd-sign.png'
                                                })
                                            }
                                            if (res.ds[i].BNType == 3) {
                                                annotations.push({
                                                    id: i,
                                                    size: 40,
                                                    lon: res.ds[i].Longitude,
                                                    lat: res.ds[i].Latitude,
                                                    icon: 'widget://image/customer-sign.png'
                                                })
                                            }
                                            if (res.ds[i].BNType == 4) {
                                                annotations.push({
                                                    id: i,
                                                    size: 40,
                                                    lon: res.ds[i].Longitude,
                                                    lat: res.ds[i].Latitude,
                                                    icon: 'widget://image/zj-sign.png'
                                                })
                                            }

                                        }



                                        map.addAnnotations({
                                            annotations: annotations,
                                            // icon: 'widget://image/customer-sign.png',
                                            draggable: false
                                        }, function (ret) {
                                            if (ret) {

                                                var frames = api.frames();
                                                var new_frame = frames.filter(function (row) {
                                                    return row.name == 'signData';
                                                })
                                                if (new_frame.length == 0) {
                                                    var appTop_ = parseInt(window.localStorage.getItem('safeAreaTop'));
                                                    var y = appTop_ ? (appTop_ + 40) : (appTop_ + 25);
                                                    api.openFrame({
                                                        name: 'signData',
                                                        url: 'common/signData.html',
                                                        rect: {
                                                            x: 5,
                                                            y: y + 5,
                                                            w: api.winWidth - 10,
                                                            h: 96
                                                        },
                                                        pageParam: {
                                                            signData: res.ds[ret.id]
                                                        }
                                                    });
                                                } else {
                                                    api.sendEvent({
                                                        name: 'setSignData',
                                                        extra: {
                                                            signData: res.ds[ret.id]
                                                        }
                                                    });
                                                }




                                            }
                                        });
                                    })
                                }
                            });


                            api.openFrame({
                                name: 'userLocation',
                                url: 'component/userLocation.html',
                                rect: {
                                    x: 10,
                                    y: api.winHeight - 60,
                                    w: 40,
                                    h: 40
                                }
                            });

                            api.openFrame({
                                name: 'signExamp',
                                url: 'component/signExamp.html',
                                rect: {
                                    x: 60,
                                    y: api.winHeight - 60,
                                    w: 260,
                                    h: 40
                                }
                            });
                        } else if (api.pageParam.type == '商城分布') {
                            var ifFirst = true;
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: false
                            }, function (ret, err) {
                                if (ret.status) {

                                    if (!ifFirst) {
                                        map.setAnnotationCoords({
                                            id: 9999,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        });
                                        return false;
                                    }
                                    ifFirst = false;



                                    map.addAnnotations({
                                        annotations: [{
                                            id: 9999,
                                            size: 40,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }],
                                        icon: 'widget://image/u.png',
                                        draggable: true
                                    }, function (ret) {
                                        if (ret) {
                                            // setPopup();
                                        }
                                    });


                                    map.setCenter({
                                        coords: {
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }
                                    });

                                    var rangeJSON = that.calLatLngByDistance(ret.lat, ret.lon, 500);

                                    var obj = {
                                        keyword: '',
                                        RegionId: 0,
                                        RegionName: ''
                                    }
                                    FLD.ajax('sp_searchClientUserByArea', obj, function (res) {
                                        var annotations = [];
                                        for (var i in res.ds) {
                                            if (res.ds[i].Latlng) {
                                                var array = res.ds[i].Latlng.split(',');
                                                annotations.push({
                                                    id: i,
                                                    size: 40,
                                                    lon: array[0],
                                                    lat: array[1],
                                                    icon: 'widget://image/customer-sign.png'
                                                })
                                            }


                                        }



                                        map.addAnnotations({
                                            annotations: annotations,
                                            // icon: 'widget://image/customer-sign.png',
                                            draggable: false
                                        }, function (ret) {
                                            if (ret) {

                                                var frames = api.frames();
                                                var new_frame = frames.filter(function (row) {
                                                    return row.name == 'signData_Shopping';
                                                })
                                                if (new_frame.length == 0) {
                                                    var appTop_ = parseInt(window.localStorage.getItem('safeAreaTop'));
                                                    var y = appTop_ ? (appTop_ + 40) : (appTop_ + 25);
                                                    api.openFrame({
                                                        name: 'signData_Shopping',
                                                        url: 'common/signData_Shopping.html',
                                                        rect: {
                                                            x: 5,
                                                            y: y + 5,
                                                            w: api.winWidth - 10,
                                                            h: 96
                                                        },
                                                        pageParam: {
                                                            signData: res.ds[ret.id]
                                                        }
                                                    });
                                                } else {
                                                    api.sendEvent({
                                                        name: 'setSignData',
                                                        extra: {
                                                            signData: res.ds[ret.id]
                                                        }
                                                    });
                                                }




                                            }
                                        });
                                    })
                                }
                            });


                            api.openFrame({
                                name: 'userLocation',
                                url: 'component/userLocation.html',
                                rect: {
                                    x: 10,
                                    y: api.winHeight - 60,
                                    w: 40,
                                    h: 40
                                }
                            });
                        } else if (api.pageParam.type == '考勤') {
                            var ifFirst = true;
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: false
                            }, function (ret, err) {
                                if (ret.status) {

                                    if (!ifFirst) {
                                        map.setAnnotationCoords({
                                            id: 9999,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        });
                                        return false;
                                    }
                                    ifFirst = false;

                                    map.addAnnotations({
                                        annotations: [{
                                            id: 9999,
                                            size: 40,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }],
                                        icon: 'widget://image/u.png',
                                        draggable: true
                                    }, function (ret) {
                                        if (ret) {

                                        }
                                    });

                                    map.setCenter({
                                        coords: {
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }
                                    });

                                    var rangeJSON = that.calLatLngByDistance(ret.lat, ret.lon, 500);

                                    api.openFrame({
                                        name: 'clockForm',
                                        url: 'oa/clockForm.html',
                                        rect: {
                                            x: 0,
                                            y: api.winHeight - 190,
                                            w: api.winWidth,
                                            h: 190
                                        },
                                        pageParam: {
                                            punchClockType: api.pageParam.punchClockType
                                        }
                                    });
                                }
                            });


                            api.openFrame({
                                name: 'userLocation',
                                url: 'component/userLocation.html',
                                rect: {
                                    x: 10,
                                    y: api.winHeight - 250,
                                    w: 40,
                                    h: 40
                                }
                            });




                        } else if (api.pageParam.type == '签到') {
                            var ifFirst = true;
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: false
                            }, function (ret, err) {
                                if (ret.status) {

                                    if (!ifFirst) {
                                        map.setAnnotationCoords({
                                            id: 9999,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        });
                                        return false;
                                    }
                                    ifFirst = false;

                                    map.addAnnotations({
                                        annotations: [{
                                            id: 9999,
                                            size: 40,
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }],
                                        icon: 'widget://image/u.png',
                                        draggable: true
                                    }, function (ret) {
                                        if (ret) {

                                        }
                                    });

                                    map.setCenter({
                                        coords: {
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }
                                    });

                                    var rangeJSON = that.calLatLngByDistance(ret.lat, ret.lon, 500);

                                    if (api.pageParam.ClientID) {
                                        var obj = {
                                            S_Type: 16,
                                            S_Cont: api.pageParam.ClientID,
                                            S_BranchID: userInfo.S_BranchID,
                                            S_SXZ: '',
                                            S_StockID: userInfo.S_StockID,
                                            S_CargoID: userInfo.S_CargoID,
                                            S_BarCode: '',
                                            S_PriceN: userInfo.S_PriceN,
                                            S_ProductID: 0
                                        }
                                        FLD.ajax('AP_BasList', obj, function (res) {
                                            var annotations = [];
                                            var z = false;
                                            for (var i in res.ds) {
                                                if (res.ds[i].Longitude && res.ds[i].Latitude) {
                                                    if (res.ds[i].Longitude > rangeJSON.newLng1 && res.ds[i].Longitude < rangeJSON.newLng2 && res.ds[i].Latitude > rangeJSON.newLat1 && res.ds[i].Latitude < rangeJSON.newLat2) {
                                                        z = true;
                                                    }
                                                    annotations.push({
                                                        id: i,
                                                        size: 40,
                                                        lon: res.ds[i].Longitude,
                                                        lat: res.ds[i].Latitude
                                                    })
                                                }

                                            }
                                            api.openFrame({
                                                name: 'customerSignForm',
                                                url: 'trip/customerSignForm.html',
                                                rect: {
                                                    x: 0,
                                                    y: api.winHeight - 190,
                                                    w: api.winWidth,
                                                    h: 190
                                                },
                                                pageParam: {
                                                    defaultCustomerData: res.ds[0],
                                                    historyData: api.pageParam.historyData,
                                                    isAllowSign: z
                                                }
                                            });
                                            api.openFrame({
                                                name: 'customerData',
                                                url: 'common/customerData.html',
                                                rect: {
                                                    x: 5,
                                                    y: y + 5,
                                                    w: api.winWidth - 10,
                                                    h: 96
                                                },
                                                pageParam: {
                                                    customerData: res.ds[0]
                                                }
                                            });


                                            map.addAnnotations({
                                                annotations: annotations,
                                                icon: 'widget://image/customer-sign.png',
                                                draggable: false
                                            }, function (ret) {
                                                if (ret) {


                                                }
                                            });
                                        })
                                    } else {


                                        //拜访新增需求
                                        var obj = {
                                            // S_Type: 17,
                                            // S_Cont: '',
                                            // S_BranchID: userInfo.S_BranchID,
                                            // S_SXZ: '',
                                            // S_StockID: userInfo.S_StockID,
                                            // S_CargoID: userInfo.S_CargoID,
                                            // S_BarCode: '',
                                            // S_PriceN: userInfo.S_PriceN,
                                            // S_ProductID: 0,


                                            S_NBranchID: api.pageParam.trip.S_NBranchID,
                                            S_BHID: api.pageParam.trip.S_BHID,
                                            S_ClientID: '',
                                            S_DQDate: api.pageParam.trip.S_SDate,
                                            S_ARName: -123
                                        }
                                        FLD.ajax('APP_DBHKHLT', obj, function (res) {
                                            var annotations = [];
                                            console.log(JSON.stringify(res))
                                            var distanceArray = [];
                                            var z = -1;
                                            for (var i in res.ds) {
                                                if (res.ds[i].Longitude && res.ds[i].Latitude) {
                                                    if (res.ds[i].Longitude > rangeJSON.newLng1 && res.ds[i].Longitude < rangeJSON.newLng2 && res.ds[i].Latitude > rangeJSON.newLat1 && res.ds[i].Latitude < rangeJSON.newLat2) {
                                                        z = i;
                                                    }


                                                    distanceArray.push({
                                                        d: Math.abs(res.ds[i].Latitude - ret.lat) + Math.abs(res.ds[i].Longitude - ret.lon),
                                                        index: i
                                                    })
                                                    annotations.push({
                                                        id: i,
                                                        size: 40,
                                                        lon: res.ds[i].Longitude,
                                                        lat: res.ds[i].Latitude
                                                    })
                                                }

                                            }
                                            if (z > -1) {
                                                api.openFrame({
                                                    name: 'customerSignForm',
                                                    url: 'trip/customerSignForm.html',
                                                    rect: {
                                                        x: 0,
                                                        y: api.winHeight - 190,
                                                        w: api.winWidth,
                                                        h: 190
                                                    },
                                                    pageParam: {
                                                        defaultCustomerData: res.ds[z],
                                                        isAllowSign: true,
                                                        trip: api.pageParam.trip
                                                    }
                                                });
                                                // api.openFrame({
                                                //     name: 'customerData',
                                                //     url: 'common/customerData.html',
                                                //     rect: {
                                                //         x: 5,
                                                //         y: y + 5,
                                                //         w: api.winWidth - 10,
                                                //         h: 96
                                                //     },
                                                //     pageParam: {
                                                //         customerData: res.ds[z]
                                                //     }
                                                // });



                                            } else {
                                                var dd = distanceArray.sort(function (a, b) {
                                                    return a.d - b.d
                                                })
                                                api.openFrame({
                                                    name: 'customerSignForm',
                                                    url: 'trip/customerSignForm.html',
                                                    rect: {
                                                        x: 0,
                                                        y: api.winHeight - 190,
                                                        w: api.winWidth,
                                                        h: 190
                                                    },
                                                    pageParam: {
                                                        defaultCustomerData: res.ds[dd[0].index],
                                                        isAllowSign: false,
                                                        trip: api.pageParam.trip
                                                    }
                                                });
                                                // api.openFrame({
                                                //     name: 'customerData',
                                                //     url: 'common/customerData.html',
                                                //     rect: {
                                                //         x: 5,
                                                //         y: y + 5,
                                                //         w: api.winWidth - 10,
                                                //         h: 96
                                                //     },
                                                //     pageParam: {
                                                //         customerData: res.ds[dd[0].index],
                                                //     }
                                                // });
                                            }
                                            map.addAnnotations({
                                                annotations: annotations,
                                                icon: 'widget://image/customer-sign.png',
                                                draggable: false
                                            }, function (ret) {
                                                if (ret) {


                                                }
                                            });

                                        })
                                    }



                                }
                            });


                            api.openFrame({
                                name: 'userLocation',
                                url: 'component/userLocation.html',
                                rect: {
                                    x: 10,
                                    y: api.winHeight - 250,
                                    w: 40,
                                    h: 40
                                }
                            });




                        } else if (api.pageParam.type == '定位') {
                            api.openFrame({
                                name: 'sign',
                                url: 'component/sign.html',
                                rect: {
                                    x: (api.winWidth / 2) - 15,
                                    y: ((api.winHeight - y) / 2) + y - 26,
                                    w: 30,
                                    h: 30
                                }
                            });
                            if (api.pageParam.saveType == '商城') {

                                if (that.customerData.Latlng) {
                                    var Latlng = that.customerData.Latlng.split(',');
                                    map.setCenter({
                                        coords: {
                                            lon: Latlng[0],
                                            lat: Latlng[1]
                                        }
                                    });

                                } else {
                                    map.getLocation({
                                        accuracy: '10m',
                                        autoStop: true
                                    }, function (ret, err) {
                                        if (ret.status) {

                                            map.setCenter({
                                                coords: {
                                                    lon: ret.lon,
                                                    lat: ret.lat
                                                }
                                            });
                                        }
                                    });

                                }
                            } else {
                                if (that.customerData.Longitude && that.customerData.Latitude) {
                                    map.setCenter({
                                        coords: {
                                            lon: that.customerData.Longitude,
                                            lat: that.customerData.Latitude
                                        }
                                    });

                                } else {
                                    map.getLocation({
                                        accuracy: '10m',
                                        autoStop: true
                                    }, function (ret, err) {
                                        if (ret.status) {

                                            map.setCenter({
                                                coords: {
                                                    lon: ret.lon,
                                                    lat: ret.lat
                                                }
                                            });
                                        }
                                    });

                                }
                            }

                        } else if (api.pageParam.type == '记录') {



                            var obj = {
                                S_UserID: userInfo.UserID,
                                S_SDate: FLD.getFormatDate().split(' ')[0],
                                S_EDate: FLD.getFormatDate().split(' ')[0],
                                S_ClientID: ''
                            }

                            FLD.ajax('APP_KQJLJK_N', obj, function (res) {
                                var visitList = [];
                                for (var i in res.ds) {
                                    if (res.ds[i].NMStatus == '开始打卡') {
                                        var x = 0;
                                        for (var j in res.ds) {
                                            if (res.ds[j].NMStatus == '结束打卡' && res.ds[j].ClientID == res.ds[i].ClientID && res.ds[j].UserID == res.ds[i].UserID) {
                                                x = 1;
                                                visitList.push({
                                                    UserName: res.ds[i].UserName,
                                                    ClientName: res.ds[i].ClientName,
                                                    StartTime: res.ds[i].BDate,
                                                    EndTime: res.ds[j].BDate,
                                                    FRemarks1: res.ds[i].FRemarks,
                                                    FRemarks2: res.ds[j].FRemarks,
                                                    PIC_Path1: res.ds[i].PIC_Path,
                                                    PIC_Path2: res.ds[j].PIC_Path,
                                                    lon: res.ds[i].Longitude,
                                                    lat: res.ds[i].Latitude
                                                })
                                                break;
                                            }
                                        }
                                        if (x == 0) {
                                            visitList.push({
                                                UserName: res.ds[i].UserName,
                                                ClientName: res.ds[i].ClientName,
                                                StartTime: res.ds[i].BDate,
                                                EndTime: '',
                                                FRemarks1: res.ds[i].FRemarks,
                                                FRemarks2: '',
                                                PIC_Path1: res.ds[i].PIC_Path,
                                                PIC_Path2: '',
                                                lon: res.ds[i].Longitude,
                                                lat: res.ds[i].Latitude
                                            })
                                        }
                                    }
                                }

                                if (!visitList.length) {
                                    map.getLocation({
                                        accuracy: '100m',
                                        autoStop: true
                                    }, function (ret, err) {
                                        if (ret.status) {
                                            map.setCenter({
                                                coords: {
                                                    lon: ret.lon,
                                                    lat: ret.lat
                                                }
                                            });
                                        }
                                    })
                                } else {
                                    map.getLocation({
                                        accuracy: '100m',
                                        autoStop: true
                                    }, function (ret, err) {
                                        if (ret.status) {
                                            map.setCenter({
                                                coords: {
                                                    lon: visitList[0].lon,
                                                    lat: visitList[0].lat
                                                }
                                            });
                                        }
                                    })
                                }


                                for (var i in visitList) {
                                    visitList[i].id = parseInt(i) + 1;
                                    visitList[i].size = 40;
                                    visitList[i].icon = 'widget://image/visit/visit-' + visitList[i].id + '.png';
                                }

                                map.addAnnotations({
                                    annotations: visitList,
                                    draggable: false
                                }, function (ret) {
                                    if (ret) {
                                        map.setBubble({
                                            id: ret.id,
                                            content: {
                                                title: visitList[ret.id - 1].ClientName,
                                                subTitle: '签到：' + visitList[ret.id - 1].StartTime.split(' ')[1] + ' - ' + (visitList[ret.id - 1].EndTime.split(' ')[1] || '')
                                            },
                                            styles: {
                                                titleColor: '#000',
                                                titleSize: 14,
                                                subTitleColor: '#999',
                                                subTitleSize: 12,
                                                illusAlign: 'left',
                                                w: that.getStringLength(visitList[ret.id - 1].ClientName) * 17,
                                                h: 100
                                            }
                                        }, function (ret) {

                                        });
                                        map.popupBubble({
                                            id: ret.id
                                        });

                                    }
                                });

                                setTimeout(function () {
                                    if (visitList.length) {
                                        map.setBubble({
                                            id: 1,
                                            content: {
                                                title: visitList[0].ClientName,
                                                subTitle: '签到：' + visitList[0].StartTime.split(' ')[1] + ' - ' + (visitList[0].EndTime.split(' ')[1] || '')
                                            },
                                            styles: {
                                                titleColor: '#000',
                                                titleSize: 14,
                                                subTitleColor: '#999',
                                                subTitleSize: 12,
                                                illusAlign: 'left',
                                                w: that.getStringLength(visitList[0].ClientName) * 17,
                                                h: 100
                                            }
                                        }, function (ret) {

                                        });
                                        map.popupBubble({
                                            id: 1
                                        });
                                    }

                                }, 1000)


                                api.openFrame({
                                    name: 'customerVisitList',
                                    url: 'customerVisitList.html',
                                    rect: {
                                        x: 0,
                                        y: api.winHeight - 190,
                                        w: api.winWidth,
                                        h: 190
                                    },
                                    pageParam: {
                                        visitList: visitList
                                    }
                                });

                                api.openFrame({
                                    name: 'sildeArrow',
                                    url: 'component/slideArrow.html',
                                    rect: {
                                        x: (api.winWidth / 2) - 20,
                                        y: api.winHeight - 210,
                                        w: 40,
                                        h: 40
                                    }
                                });


                                map.searchRoute({
                                    id: 999,
                                    type: 'walk',
                                    start: visitList[0],
                                    end: visitList[visitList.length - 1],
                                    waypoints: visitList.slice(1, visitList.length - 1)
                                }, function (ret, err) {
                                    if (ret.status) {
                                        map.drawRoute({
                                            id: 999,
                                            index: 0,
                                            styles: {
                                                line: {
                                                    color: '#5298ff',
                                                    width: 2,
                                                    dash: false,
                                                    textureImg: ''
                                                },
                                                start: {
                                                    icon: 'widget://image/kong.png'
                                                },
                                                node: {
                                                    icon: 'widget://image/kong.png'
                                                },
                                                end: {
                                                    icon: 'widget://image/kong.png'
                                                }
                                            }
                                        }, function (ret) {

                                        });

                                    }
                                });


                            })


                        }

                    });
                },
                searchVisitList(date) {
                    var that = this;
                    var obj = {
                        S_UserID: userInfo.UserID,
                        S_SDate: date,
                        S_EDate: date,
                        S_ClientID: ''
                    }

                    FLD.ajax('APP_KQJLJK_N', obj, function (res) {
                        var visitList = [];
                        for (var i in res.ds) {
                            if (res.ds[i].NMStatus == '开始打卡') {
                                var x = 0;
                                for (var j in res.ds) {
                                    if (res.ds[j].NMStatus == '结束打卡' && res.ds[j].ClientID == res.ds[i].ClientID && res.ds[j].UserID == res.ds[i].UserID) {
                                        x = 1;
                                        visitList.push({
                                            UserName: res.ds[i].UserName,
                                            ClientName: res.ds[i].ClientName,
                                            StartTime: res.ds[i].BDate,
                                            EndTime: res.ds[j].BDate,
                                            FRemarks1: res.ds[i].FRemarks,
                                            FRemarks2: res.ds[j].FRemarks,
                                            PIC_Path1: res.ds[i].PIC_Path,
                                            PIC_Path2: res.ds[j].PIC_Path,
                                            lon: res.ds[i].Longitude,
                                            lat: res.ds[i].Latitude
                                        })
                                        break;
                                    }
                                }
                                if (x == 0) {
                                    visitList.push({
                                        UserName: res.ds[i].UserName,
                                        ClientName: res.ds[i].ClientName,
                                        StartTime: res.ds[i].BDate,
                                        EndTime: '',
                                        FRemarks1: res.ds[i].FRemarks,
                                        FRemarks2: '',
                                        PIC_Path1: res.ds[i].PIC_Path,
                                        PIC_Path2: '',
                                        lon: res.ds[i].Longitude,
                                        lat: res.ds[i].Latitude
                                    })
                                }
                            }
                        }

                        if (!visitList.length) {
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: true
                            }, function (ret, err) {
                                if (ret.status) {
                                    map.setCenter({
                                        coords: {
                                            lon: ret.lon,
                                            lat: ret.lat
                                        }
                                    });
                                }
                            })
                        } else {
                            map.getLocation({
                                accuracy: '100m',
                                autoStop: true
                            }, function (ret, err) {
                                if (ret.status) {
                                    map.setCenter({
                                        coords: {
                                            lon: visitList[0].lon,
                                            lat: visitList[0].lat
                                        }
                                    });
                                }
                            })
                        }

                        for (var i in visitList) {
                            visitList[i].id = parseInt(i) + 1;
                            visitList[i].size = 40;
                            visitList[i].icon = 'widget://image/visit/visit-' + visitList[i].id + '.png';
                        }

                        map.removeAnnotations();

                        map.addAnnotations({
                            annotations: visitList,
                            draggable: false
                        }, function (ret) {
                            if (ret) {
                                map.setBubble({
                                    id: ret.id,
                                    content: {
                                        title: visitList[ret.id - 1].ClientName,
                                        subTitle: '签到：' + visitList[ret.id - 1].StartTime.split(' ')[1] + ' - ' + (visitList[ret.id - 1].EndTime.split(' ')[1] || '')
                                    },
                                    styles: {
                                        titleColor: '#000',
                                        titleSize: 14,
                                        subTitleColor: '#999',
                                        subTitleSize: 12,
                                        illusAlign: 'left',
                                        w: that.getStringLength(visitList[ret.id - 1].ClientName) * 17,
                                        h: 100
                                    }
                                }, function (ret) {

                                });
                                map.popupBubble({
                                    id: ret.id
                                });

                            }
                        });

                        setTimeout(function () {
                            if (visitList.length) {
                                map.setBubble({
                                    id: 1,
                                    content: {
                                        title: visitList[0].ClientName,
                                        subTitle: '签到：' + visitList[0].StartTime.split(' ')[1] + ' - ' + (visitList[0].EndTime.split(' ')[1] || '')
                                    },
                                    styles: {
                                        titleColor: '#000',
                                        titleSize: 14,
                                        subTitleColor: '#999',
                                        subTitleSize: 12,
                                        illusAlign: 'left',
                                        w: that.getStringLength(visitList[0].ClientName) * 17,
                                        h: 100
                                    }
                                }, function (ret) {

                                });
                                map.popupBubble({
                                    id: 1
                                });
                            }

                        }, 1000)


                        api.sendEvent({
                            name: 'setVisitList',
                            extra: {
                                visitList: visitList
                            }
                        });



                        map.removeRoute({
                            ids: [1]
                        });




                        map.searchRoute({
                            id: 1,
                            type: 'drive',
                            policy: 'ecar_dis_first',
                            start: visitList[0],
                            end: visitList[visitList.length - 1],
                            waypoints: visitList.slice(1, visitList.length - 1)
                        }, function (ret, err) {
                            if (ret.status) {
                                // map.drawRoute({
                                //     id: 1,
                                //     index: 0,
                                //     styles: {
                                //         start: {
                                //             icon: 'widget://image/kong.png'
                                //         },
                                //         node: {
                                //             icon: 'widget://image/kong.png'
                                //         },
                                //         end: {
                                //             icon: 'widget://image/kong.png'
                                //         }
                                //     }
                                // }, function (ret) {

                                // });

                            }
                        });
                    })
                },
                calLatLngByDistance(originLat, originLng, distance) {
                    //计算距离
                    var getDistance = ($lng1, $lat1, $lng2, $lat2) => {
                        const $earthRadius = 6367000;
                        $lat1 = ($lat1 * Math.PI) / 180;
                        $lng1 = ($lng1 * Math.PI) / 180;
                        $lat2 = ($lat2 * Math.PI) / 180;
                        $lng2 = ($lng2 * Math.PI) / 180;
                        const $calcLongitude = $lng2 - $lng1;
                        const $calcLatitude = $lat2 - $lat1;
                        const $stepOne = Math.pow(Math.sin($calcLatitude / 2), 2) + Math.cos($lat1) * Math.cos($lat2) * Math.pow(Math.sin($calcLongitude / 2), 2);
                        const $stepTwo = 2 * Math.asin(Math.min(1, Math.sqrt($stepOne)));
                        const $calculatedDistance = ($earthRadius * $stepTwo);
                        return Math.round($calculatedDistance);
                    }
                    let newLat1, newLat2, newLng1, newLng2

                    let latDistance = getDistance(originLng, 1, originLng, 2);
                    newLat1 = (originLat - (distance / latDistance)).toFixed(8);
                    newLat2 = (originLat + (distance / latDistance)).toFixed(8);
                    let lngDistance = getDistance(1, originLat, 2, originLat);
                    newLng1 = (originLng - (distance / lngDistance)).toFixed(8);
                    newLng2 = (originLng + (distance / lngDistance)).toFixed(8);

                    return {
                        newLat1: newLat1,
                        newLat2: newLat2,
                        newLng1: newLng1,
                        newLng2: newLng2
                    }

                },
                saveLocation() {
                    var that = this;
                    map.getCenter(function (ret) {
                        var saveLocation_ = function () {
                            map.getNameFromCoords({
                                lon: ret.lon,
                                lat: ret.lat
                            }, function (ret, err) {
                                var address = ''
                                if (ret.status) {
                                    address = ret.address;
                                }
                                if (api.pageParam.saveType == '商城') {
                                    var obj = {
                                        Latlng: ret.lon + ',' + ret.lat,
                                        h_userId: that.customerData.UserId
                                    }
                                    FLD.ajax('sp_updateClientInfo', obj, function (res) {
                                        that.customerData.Latlng = obj.Latlng;
                                        api.toast({
                                            msg: '保存成功',
                                            duration: 2000,
                                            location: 'bottom'
                                        });
                                        api.sendEvent({
                                            name: 'setCustomerData',
                                            extra: {
                                                userData: that.customerData
                                            }
                                        });
                                        api.sendEvent({
                                            name: 'setCustomerListData',
                                            extra: {
                                                userData: that.customerData
                                            }
                                        });
                                        setTimeout(function () {
                                            api.closeWin();
                                        }, 2000)
                                    })
                                } else {
                                    var obj = {
                                        S_ClientID: that.customerData.id,
                                        S_LinkMan: that.customerData.LinkMan,
                                        S_Tel: that.customerData.NTel,
                                        S_Address: that.customerData.Address || address,
                                        S_Longitude: ret.lon,
                                        S_Latitude: ret.lat,
                                        S_Distance: that.customerData.Distance,
                                        S_ARName: that.customerData.ARName
                                    }
                                    FLD.ajax('APP_UPTClientN', obj, function (res) {
                                        that.customerData.Longitude = ret.lon;
                                        that.customerData.Latitude = ret.lat;
                                        if (!that.customerData.Address) {
                                            that.customerData.Address = address;
                                        }
                                        api.toast({
                                            msg: '保存成功',
                                            duration: 2000,
                                            location: 'bottom'
                                        });
                                        api.sendEvent({
                                            name: 'setCustomerData',
                                            extra: {
                                                customerData: that.customerData
                                            }
                                        });
                                        api.sendEvent({
                                            name: 'setCustomerListData',
                                            extra: {
                                                customerData: that.customerData
                                            }
                                        });
                                        setTimeout(function () {
                                            api.closeWin();
                                        }, 2000)
                                    })
                                }

                            });
                        }
                        if (that.customerData.C_Longitude && that.customerData.C_Latitude) {
                            map.searchRoute({
                                id: 10000,
                                type: 'drive',
                                policy: 'ecar_dis_first',
                                start: {
                                    lon: that.customerData.C_Longitude,
                                    lat: that.customerData.C_Latitude
                                },
                                end: {
                                    lon: ret.lon,
                                    lat: ret.lat
                                }
                            }, function (ret, err) {
                                if (ret.status) {

                                    that.customerData.Distance = (ret.plans[0].distance / 1000).toFixed(2);
                                    saveLocation_();
                                } else {
                                    api.toast({
                                        msg: '仓库定位错误!',
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                }
                            });
                        } else {
                            saveLocation_();
                        }
                    });

                }
            },
            onClickLeft() {
                api.closeWin();
            }
        })
    };
</script>

</html>